package com.example.neusoft.yuejian_12.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.example.neusoft.yuejian_12.Adapter.HomeAdapter;
import com.example.neusoft.yuejian_12.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class HomeFragment extends Fragment {
    private View view=null;
    private TabLayout tabs;
    private FrameLayout train_frame;
    private ViewPager train_vp;
    private int current;
    private Context context;
    //上一次图片的位置
    private int oldpos;
    private ViewPager viewPager;
    private List<String> mTitle = new ArrayList<String>();
    private List<Fragment> mFragment = new ArrayList<Fragment>();
    private List<ImageView> images;
    private int[] imgResource={R.drawable.kk,R.drawable.gg,R.drawable.pp,R.drawable.w};


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view =  inflater.inflate(R.layout.fragment_h, container, false);
        initViews();
        initEvents();
        initData();
        lunbo();
        HomeAdapter adapter = new HomeAdapter(getChildFragmentManager(), mTitle, mFragment);
        viewPager.setAdapter(adapter);
        //为TabLayout设置ViewPager
        tabs.setupWithViewPager(viewPager);
        //使用ViewPager的适配器
//        tabs.setTabsFromPagerAdapter(adapter);

        return view;
    }
    private void lunbo() {
        train_vp.setAdapter(pageadapter);
    }
    private void initViews() {

        tabs = (TabLayout) view.findViewById(R.id.tab02);
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        train_vp=(ViewPager)view.findViewById(R.id.train_vp);
        images=new ArrayList<ImageView>();
        for (int i=0;i<imgResource.length;i++)
        {
            ImageView imageView=new ImageView(getActivity());
            imageView.setBackgroundResource(imgResource[i]);
            images.add(imageView);
        }
    }
    PagerAdapter pageadapter=new PagerAdapter() {
        @Override
        public int getCount() {
            return images.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(images.get(position));
            return images.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(images.get(position));
        }
    };
    class  PageTask implements Runnable{
        @Override
        public void run() {
            current=(current+1)%imgResource.length;
            handler.sendEmptyMessage(current);
        }
    }
    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            train_vp.setCurrentItem(current);
        };
    };

    @Override
    public void onStart() {
        super.onStart();
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                current=(current+1)%imgResource.length;
                handler.sendEmptyMessage(current);
            }
        },3000,5000);

    }

    private void initEvents(){

    }

    private void initData(){
        mTitle.add("精彩活动");
        mTitle.add("权威大师");
        mTitle.add("最近比赛");
        mFragment.add(new ActivityFragment());
        mFragment.add(new MasterFragment());
        mFragment.add(new ContestFragment());
     /*   getUserId();*/
    }
   /* private void getUserId(){
        Intent intent = getActivity().getIntent();
        userid = intent.getStringExtra("uid");
//        Toast.makeText(getActivity(),userid,Toast.LENGTH_SHORT).show();
    }*/
}